"use strict";

app.controller("layoutCtrl", [
    "$scope",
    "$http",
    "$stateParams",
    "$rootScope",
    "$modal",
    "lodash",
    "$location",
    "$anchorScroll",
    "$timeout",
    function(
        $scope,
        $http,
        $stateParams,
        $rootScope,
        $modal,
        lodash,
        $location,
        $anchorScroll,
        $timeout
    ) {
        $scope.info = {
          isIssueSort : false,
          isFlowSort  : false,
          isFunSort   : false
        };

        var backUrl = {
            show: true,
            label: "返回",
            func: function() {
                $rootScope.backHistory();
            }
        };
        $rootScope.initBreadcrumb($scope.breadcrumb);
        //页面加载时，给headerElement设置元素
        $rootScope.initHeaderElements($scope.settings);
        $scope.settings.btnEle.push(backUrl);
        //结束headerElement设置
        setStorage("brdBackUrl", $location.url());

        $scope.reqDetails = {};
        var url =
            matrix_product_url +
            "/brdRequirement/getBrdRequirements?withFeatureDesc=0&reqId=" +
            $stateParams.reqId;
        $http.get(url).success(function(result) {
            if (result.code == "0") {
                if (result.data) {
                    $scope.reqDetails = result.data[0];
                    $scope.breadcrumb.push("产品设计", $scope.reqDetails.reqName);
                }
            }
        });
        //交付列表参数
        $scope.info.deliver = {
            thData: [
                { thName: "版本号" },
                { thName: "日期" },
                { thName: "操作", isHide: true }
            ],
            page: { isTrue: false, addIsTrue: true },
            list: { tdName: [{ name: "version" }, { name: "createTime" }] }
        };


        $scope.getDeliver = function() {
            var url =
                matrix_product_url +
                "/brdRequirement/getReqSubmitHistory?reqId=" +
                $stateParams.reqId;
            $http.get(url).success(function(result) {
                $timeout(function() {
                    if (result.code == "0") {
                        $scope.info.deliver.list.datas = result.data;
                        angular.forEach($scope.info.deliver.list.datas, function(res) {
                            res.createTime = timestampToTime(res.createTime);
                        });
                    } else {
                        $rootScope.toasterErrorInfo(result.desc);
                    }
                }, 500);
            });
        };

        $scope.getDeliver();

        $scope.submit = function() {
            var confirmParams = {
                titleData: "提示",
                bodyData: "确认定版交付!"
            };
            $rootScope.confirmWindow(confirmParams, function(data) {
                if (data) {
                    $http
                        .get(
                            matrix_product_url +
                            "/brdRequirement/submit?reqId=" +
                            $stateParams.reqId +
                            "&tokenId=" +
                            getTokenId()
                        )
                        .success(function(result) {
                            if (result.code == 0) {
                                $scope.getDeliver();
                                $rootScope.toasterSuccessInfo("交付成功!");
                            } else {
                                $rootScope.showMessage("error", "失败", result.desc);
                            }
                        });
                }
            });
        };

        $scope.isSort = function(isSort,type) {
          if("issue"==type){
              $scope.info.isIssueSort=!isSort;
          }else if("flow"==type){
               $scope.info.isFlowSort=!isSort;
          }else if("fun"==type){
              $scope.info.isFunSort=!isSort;
          }

        }
        // ----需求操作----
        // 需求列表参数
        $scope.info.req = {
            check: false, //显示列表复选框
            index: true, //显示序号
            thData: [{ "thName": "名称" }],
            page: { "isTrue": false, "addIsTrue": true },
            list: {
                "isOperation": {
                    "Delete": { "isTrue": true, "msg": "删除用户故事" },
                },
                "tdName": [{ "name": "action" }]
            }
        };
        $scope.info.datas = [];
        getIssueList();

        function getIssueList() {
            var url =
                matrix_product_url + "/issue/getReqIssues?reqId=" + $stateParams.reqId;
            $http.get(url).success(function(result) {
                $timeout(function() {
                    if (result.code == "0") {
                        angular.forEach(result.data, function(data, index) {
                            data.index = index;
                        });
                        $scope.info.req.list.datas = result.data;
                    } else {
                        $rootScope.toasterErrorInfo(result.desc);
                    }
                }, 500);
            });
        }
        //删除需求
        $scope.deleteIssue = function(info) {
            var confirmParams = {
                titleData: "提示",
                bodyData: "确定要删除用户故事【" + info.action + "】吗？"
            };
            $rootScope.confirmWindow(confirmParams, function(data) {
                if (data) {
                    var url =
                        matrix_product_url + "/issue/deleteIssue?issueId=" + info.id;
                    $http.get(url).success(function(result) {
                        if (result.code == "0") {
                            $rootScope.toasterSuccessInfo("删除成功！");
                            lodash.remove($scope.info.req.list.datas, { id: info.id });
                        } else {
                            $rootScope.toasterErrorInfo(result.desc);
                        }
                    });
                }
            });
        };

        //移动执行后台操作
        $scope.issueSequenceParam=function(list) {
          if(list.length==0){
            $scope.info.isIssueSort=false;
            return;
          }
            var obj = { issueIds: [], reqId: $stateParams.reqId };
            angular.forEach(list, function(data) {
                obj.issueIds.push(data.id);
            });
            $http({
              url: matrix_product_url + "/brdRequirement/updateReqIssueSequence",
              method:'post',
              headers:{
                'Content-Type':'application/json; charset=utf-8'
              },
              data:JSON.stringify(obj)

            }).success(function (data) {
                if (data.code == 0) {
                      $rootScope.toasterSuccessInfo("保存成功!");
                      $scope.info.isIssueSort=false;
                      $scope.info.req.list.datas = list;
                  } else {
                      $rootScope.showMessage("error", "失败", data.desc);
                }
            })
        }

        //
        //流程顺序参数
        $scope.info.flow = {
            check: false, //显示列表复选框
            index: true, //显示序号
            thData: [{ thName: "名称" }],
            page: { isTrue: false, addIsTrue: true },
            list: {
                isOperation: {
                    //"add"    : {"isTrue": true, "msg": "添加需求"},
                    Delete: { isTrue: true, msg: "撤销改动" }
                },
                tdName: [{ name: "name" }]
            }
        };

        $scope.getFlowList = function() {
            var url =
                matrix_product_url + "/flow/getReqFlows?reqId=" + $stateParams.reqId;
            $http.get(url).success(function(result) {
                $timeout(function() {
                    if (result.code == "0") {
                        $scope.info.flow.list.datas = result.data;
                        angular.forEach($scope.info.flow.list.datas, function(data, index) {
                            data.index = index;
                        });
                    } else {
                        $rootScope.toasterErrorInfo(result.desc);
                    }
                }, 500);
            });
        };
        $scope.getFlowList();
        //撤销
        $scope.deleteFlow = function(info) {
            var confirmParams = {
                titleData: "提示",
                bodyData: "是否撤销流程【" + info.name + "】?"
            };
            $rootScope.confirmWindow(confirmParams, function(data) {
                if (data) {
                    $http
                        .get(
                            matrix_product_url +
                            "/flow/dissociateWithFlow?flowId=" +
                            info.id +
                            "&reqId=" +
                            $stateParams.reqId
                        )
                        .success(function(result) {
                            if (result.code == "0") {
                                $scope.getFlowList();
                                $rootScope.toasterSuccessInfo("撤销成功!");
                            } else {
                                $rootScope.toasterErrorInfo(result.desc);
                            }
                        });
                }
            });
        };
        //移动执行后台操作
        $scope. flowSequenceParam=function(list) {
            if(list.length==0){
              $scope.info.isFlowSort=false;
              return;
            }
            var obj = { flowIds: [], reqId: $stateParams.reqId };

            angular.forEach(list, function(data) {
                obj.flowIds.push(data.id);
            });
            $http({
              url: matrix_product_url + "/flow/updateSequenceOfReqFlow",
              method:'post',
              headers:{
                'Content-Type':'application/json; charset=utf-8'
              },
              data:JSON.stringify(obj)

            }).success(function (data) {
                if (data.code == 0) {
                      $rootScope.toasterSuccessInfo("保存成功!");
                      $scope.info.isFlowSort=false;
                      $scope.info.flow.list.datas = list;
                  } else {
                      $rootScope.showMessage("error", "失败", data.desc);
                }
            })
        }

        //-----功能点操作-----
        //功能点顺序参数
        $scope.info.fun = {
            check: false, //显示列表复选框
            index: true, //显示序号
            thData: [{ thName: "名称" }],
            page: { isTrue: false, addIsTrue: true },
            list: {
                isOperation: {
                    //"add"    : {"isTrue": true, "msg": "添加需求"},
                    Delete: { isTrue: $rootScope.isMatrixRole(), msg: "撤销改动" }
                },
                tdName: [{ name: "name" }]
            }
        };

        $scope.getFunList = function() {
            var url =
                matrix_product_url +
                "/feature/getAllFeatureForReq?reqId=" +
                $stateParams.reqId;
            $http.get(url).success(function(result) {
                $timeout(function() {
                    var funList = [];
                    if (result.code == "0") {
                        angular.forEach(result.data, function(data, index) {
                            funList.push({
                                id: data.id,
                                name: data.path+"/"+data.featureName,
                                index: index
                            });
                        });
                        $scope.info.fun.list.datas = funList;
                    } else {
                        $rootScope.toasterErrorInfo(result.desc);
                    }
                }, 500);
            });
        };
        $scope.getFunList();

        //移动执行后台操作
        $scope.featureSequenceParam=function(list) {
            if(list.length==0){
              $scope.info.isFunSort=false;
              return;
            }
            var obj = { featureIds: [], reqId: $stateParams.reqId };

            angular.forEach(list, function(data) {
                obj.featureIds.push(data.id);
            });

            $http({
              url: matrix_product_url + "/brdRequirement/updateReqFeatureSequence",
              method:'post',
              headers:{
                'Content-Type':'application/json; charset=utf-8'
              },
              data:JSON.stringify(obj)

            }).success(function (data) {
                if (data.code == 0) {
                      $rootScope.toasterSuccessInfo("保存成功!");
                      $scope.info.isFunSort=false;
                      $scope.info.fun.list.datas = list;
                  } else {
                      $rootScope.showMessage("error", "失败", data.desc);
                }
            })
        }

        //撤销改动，解除功能点
        $scope.delFun = function(funInfo) {
            var info = {};
            info.titleData = "提示";
            info.bodyData = "确定要重置【" + funInfo.name + "】的节点吗？";
            $rootScope.confirmWindow(info, function(data) {
                if (data) {
                    var url =
                        matrix_product_url +
                        "/feature/dissociateRelation?reqId=" +
                        $stateParams.reqId +
                        "&featureId=" +
                        funInfo.id;
                    $http.get(url).success(function(result) {
                        if (result.code == "0") {
                            $scope.getFunList();
                            $rootScope.showMessage("success", "成功", "重置成功！");
                        } else {
                            $rootScope.showMessage("error", "失败", result.desc);
                        }
                    });
                }
            });
        };
    }
]);